package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.ActivityService;
import com.atguigu.gmall.activity.service.CouponInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.activity.CouponInfo;
import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.cart.CartInfoVo;
import com.atguigu.gmall.model.order.OrderDetail;
import com.atguigu.gmall.model.order.OrderTradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("/api/activity")
public class ActivityApiController {

    //  /api/activity/findActivityAndCoupon/{skuId}
    @Autowired
    private ActivityService activityService;

    @Autowired
    private CouponInfoService couponInfoService;


    //  当商品详情页面加载的时候，获取到优惠券，促销活动的列表！
    @GetMapping("findActivityAndCoupon/{skuId}")
    public Result findActivityAndCoupon(@PathVariable Long skuId, HttpServletRequest request){
        //  需要获取到userId
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) userId="0";
        //  调用服务层方法：
        Map<String,Object> map = activityService.findActivityAndCoupon(skuId,Long.parseLong(userId));
        //  返回result 对象
        return Result.ok(map);
    }

    //  获取领用优惠券的控制器
    //  /api/activity/auth/getCouponInfo/{couponId}
    //  在网关中做过鉴权：带有auth 的时候，必须要登录处理！
    @GetMapping("auth/getCouponInfo/{couponId}")
    public Result getCouponInfo(@PathVariable Long couponId,HttpServletRequest request){
        //  需要获取到用户Id
        String userId = AuthContextHolder.getUserId(request);
        //  调用服务层方法
        activityService.getCouponInfo(couponId,userId);
        //  默认返回
        return Result.ok();
    }

    //  获取优惠券列表！
    //  /api/activity/auth/{page}/{limit}
    @GetMapping("auth/{page}/{limit}")
    public Result couponInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 HttpServletRequest request){
        //  获取到用户Id
        String userId = AuthContextHolder.getUserId(request);
        //  获取到page 对象
        Page<CouponInfo> couponInfoPage = new Page<>(page,limit);
        //  查询优惠券的列表！
        IPage<CouponInfo> infoIPage = couponInfoService.getPageByUserId(couponInfoPage,Long.parseLong(userId));
        //  返回数据
        return Result.ok(infoIPage);
    }

    //  根据购物项获取到购物车对应的促销规则与优惠券列表！
    @PostMapping("inner/findCartActivityAndCoupon/{userId}")
    public List<CartInfoVo> findCartActivityAndCoupon(@RequestBody List<CartInfo> cartInfoList,@PathVariable Long userId){
        //  findCartActivityRuleMap();
        // 获取优惠券 findCartCouponInfo(cartInfoList,skuIdToActivityIdMap,userId)
        return activityService.findCartActivityAndCoupon(cartInfoList,userId);
    }

    //  根据购物项获取到订单的促销规则与优惠券列表！
    @PostMapping("inner/findTradeActivityAndCoupon/{userId}")
    public OrderTradeVo findTradeActivityAndCoupon(@RequestBody List<OrderDetail> orderDetailList,@PathVariable Long userId){
        // Map<Long, ActivityRule> findTradeActivityRuleMap(List<OrderDetail> orderDetailList);
        // List<CouponInfo> findTradeCouponInfo(List<OrderDetail> orderDetailList, Map<Long, ActivityRule> activityIdToActivityRuleMap, Long userId);
        //  返回数据
        return activityService.findTradeActivityAndCoupon(orderDetailList,userId);
    }


    //  更新优惠券的使用状态！
    @GetMapping("inner/updateCouponInfoUseStatus/{couponId}/{userId}/{orderId}")
    public Boolean updateCouponInfoUseStatus(@PathVariable Long couponId,
                                             @PathVariable Long userId,
                                             @PathVariable Long orderId){
        //  更新优惠券的使用状态
        couponInfoService.updateCouponInfoUseStatus(couponId,userId,orderId);

        //  默认返回
        return true;

    }

}
